<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button v-allow="'import-excel'" type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button v-allow="'export-excel'" type="danger" size="small">excel导出</el-button>
          <el-button v-allow="'add-employees'" type="primary" size="small" @click.native="show = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="{ row }">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template v-slot="{ row: { id } }">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/'+id)">查看</el-button>
              <el-button type="text" size="small" @click="distributionRole(id)">分配角色</el-button>
              <el-button type="text" size="small" @click="handleRemove(id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <!-- 分页 -->
          <el-pagination
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
            :page-size="obj.size"
            :current-page="obj.page"
            :page-sizes="[2, 5, 10]"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </div>
      </el-card>
    </div>

    <!-- 弹框 -->
    <el-dialog :title="title" :visible.sync="show">
      <EmpDialog
        v-if="show"
        :show.sync="show"
        @addSuccess="addChangeData"
      />
    </el-dialog>

    <!-- 权限弹框 -->
    <el-dialog :visible.sync="assShow" title="分配角色">
      <assign-role v-if="assShow" :id="curEmployeId" :show.sync="assShow" :assign-role="assignRole" />
    </el-dialog>
  </div>
</template>

<script>
import { delEmployee, getEmployeeList } from '@/api/employees'
import EmpDialog from '@/views/employees/components/empDialog.vue'
import employees from '@/constant/employees'
import AssignRole from './components/assignRole.vue'
import { getUserDetailById } from '@/api/user'
export default {
  components: { EmpDialog, AssignRole },
  data() {
    return {
      title: '新增',
      show: false,
      list: [],
      curEmployeId: '',
      obj: {
        page: 1, // 当前页码
        size: 5 // 每页几条
      },
      total: 0,
      assShow: false,
      assignRole: []
    }
  },
  created() {
    this.loadEmployeeList()
    this.formatEmployment()
  },
  methods: {
    async distributionRole(id) {
      this.curEmployeId = id
      const { data: { roleIds }} = await getUserDetailById(id)
      // 保存这个员工当前的已经有的角色
      this.assignRole = roleIds
      this.assShow = true
    },
    addChangeData() {
      this.total++
      this.obj.page = Math.ceil(this.total / this.obj.size)
      this.loadEmployeeList()
    },
    async loadEmployeeList() {
      try {
        const { data: { rows, total }} = await getEmployeeList(this.obj)
        this.list = rows
        this.total = total
      } catch (err) {
        console.log(err)
        this.$message.error('加载员工数据失败')
      }
    },
    formatEmployment(value) {
      const obj = employees.hireType.reduce((a, b) => {
        a[b.id] = b.value
        return a
      }, {})
      return obj[value] || '未知'
    },
    hCurrentChange(val) {
      this.obj.page = val
      this.loadEmployeeList()
    },
    hSizeChange(val) {
      this.obj.size = val
      this.loadEmployeeList()
    },
    async handleRemove(id) {
      try {
        await this.$confirm('确认删除吗？')

        const { message } = await delEmployee(id)
        this.$message.success(message)

        if (this.list.length === 1) {
          this.obj.page--
          if (this.obj.page <= 0) {
            this.obj.page = 1
          }
        }

        this.loadEmployeeList()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
